/*
 * @# UserServiceImpl.java 2011-12-14 下午02:33:51
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.px.user.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ianswer.common.orm.PublicDao;
import com.ianswer.px.user.entity.User;
import com.ianswer.px.user.redis.UserRedis;
import com.ianswer.px.user.service.UserService;

/**
 * @author lunx
 */
@Service("userService")
public class UserServiceImpl implements UserService {

	@Resource(name = "publicDao")
	private PublicDao publicDao;

	@Override
	public Long insertUser(User user) {
		UserRedis.init(user);
		return (Long) publicDao.insert("users.insert", user);
	}

	@Override
	public User getUserById(Long userId) {
		return publicDao.querySingle("users.getUserById", userId);
	}

	@Override
	public User getUserByCode(String email) {
		return publicDao.querySingle("users.getUserByCode", email);
	}

	@Override
	public void updateUser(User user) {
		UserRedis.init(user);
		publicDao.update("users.updateUserBaseInfo", user);
	}

	@Override
	public void updateUserState(String userCode) {
		User user = new User();
		user.activate();
		user.setEmail(userCode);
		publicDao.update("users.updateState", user);
	}

	@Override
	public User checkUser(String userCode, String userPwd) {
		User user = new User();
		user.setEmail(userCode);
		user.setPassword(userPwd);
		return publicDao.querySingle("users.checkUser", user);
	}

	@Override
	public void updateUserPwd(String newPwd, Long id) {
		User user = new User();
		user.setId(id);
		user.setPassword(newPwd);
		publicDao.update("users.updateUserPwd", user);
	}

}
